package com.gisuni.sddc.platformManager.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.model.LayerInfo;
import com.gisuni.sddc.platformManager.model.ModuleInfo;
import com.gisuni.sddc.platformManager.model.ModuleRLayer;
import com.gisuni.sddc.platformManager.model.Parameter;
import com.gisuni.sddc.platformManager.model.ServerInfo;

@IocBean
public class ServerResourceDao {
	@Inject
	private BasicDao basicDao;
	
	@Inject
	private Dao dao;

	public List<ServerInfo> getAllServerTreeNode() {
		//状态1为可用的
		Cnd cnd = Cnd.where("STATUS", "=", "1");
		return basicDao.search(ServerInfo.class, cnd);
	}

	public List<LayerInfo> getAllLayerTreeNode() {

		return basicDao.search(LayerInfo.class, null);
	}

	public List<ModuleInfo> getAllModuleTreeNode() {
		Cnd cnd = Cnd.where("MODEL_NAME", "=", "全局配置");
//		return basicDao.search(ModuleInfo.class, null);
		return basicDao.search(ModuleInfo.class, cnd);
	}

	public List<Parameter> getAllParameters() {
		return basicDao.search(Parameter.class, null);
	}

	public List<ModuleRLayer> getConfFieldsByid(String id, String pid) {
		Cnd cnd = Cnd.where("PARAMETER_ID", "=", id);
		cnd.and("MODULE_ID", "=", pid);
		return basicDao.search(ModuleRLayer.class, cnd);
	}

	public Object getLayerInfosByIds(String serverId, String layerId) {
		Sql sql=Sqls.create("select l.ID,l.layer_index,l.server_id,s.DYNAMICS_URL from SDDCGISPLATFORM.SERVER_INFO s left join SDDCGISPLATFORM.LAYER_INFO l on s.id = l.server_id where l.server_id =@sid and l.id = @lid");
		sql.params().set("sid",serverId).set("lid",layerId);
		System.out.println(sql);
	    sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				Map<String, String> laymap=new HashMap<>();
				while(rs.next()){
					laymap.put("layId", rs.getString("ID"));
					laymap.put("layIndex", rs.getString("layer_index"));
					laymap.put("serId", rs.getString("server_id"));
					laymap.put("dUrl", rs.getString("DYNAMICS_URL"));
				}
				return laymap;
			}
		});
	    dao.execute(sql);
	    return sql.getResult();
	    
	}

	public Boolean insertOrActionServerConfig(String moduleId, String layerId, String fieldsExpress, String layerParam) {
		Cnd cnd=Cnd.where("MODULE_ID","=",moduleId).and("LAYERPARAM","=",layerParam);
		ModuleRLayer mrl=basicDao.findByCondition(ModuleRLayer.class, cnd);
		boolean flag=false;
		if(mrl==null){
			ModuleRLayer moduleRLayer=new ModuleRLayer();
			moduleRLayer.setModuleId(moduleId);
			moduleRLayer.setLayerId(layerId);
			moduleRLayer.setFields(fieldsExpress);
			moduleRLayer.setLayerParam(layerParam);
			moduleRLayer.setId(UUID.randomUUID().toString());
			basicDao.save(moduleRLayer);
			flag=true;
			return flag;
		}else{
			mrl.setFields(fieldsExpress);
			mrl.setLayerId(layerId);
			System.out.println(mrl);
			return 	basicDao.update(mrl);
		}
		
	    
	}

	public ModuleRLayer getModuleServerConfigByMAL(String moduleId, String layerParam) {
		Cnd cnd=Cnd.where("MODULE_ID","=",moduleId).and("LAYERPARAM","=",layerParam);
		ModuleRLayer mrl=basicDao.findByCondition(ModuleRLayer.class, cnd);
		return mrl;
	}
}
